/*
 * @Author: llch
 * @Date: 2020-09-17 11:17:39
 * @LastEditTime: 2021-04-17 16:04:18
 * @LastEditors: Please set LastEditors
 * @Description: 封装的动画组件，需要提供精灵组件和动画组件
 * @FilePath: \tower_defense_demo\assets\scripts\common\frame_anim.ts
 */

const {ccclass, property} = cc._decorator;

@ccclass
export class FrameAnim extends cc.Component {

    @property(cc.Sprite)
    public sprite_com: cc.Sprite = null;    // 挂载的精灵组件
    @property(cc.Animation)
    public anim_com: cc.Animation = null;   // 挂载的动画组件
    
    @property(cc.SpriteFrame)
    public sprite_frame_arr: Array<cc.SpriteFrame> = [];    // 精灵帧

    @property(cc.Float)
    public sample = 60;                   // 帧速率,值越小动画持续时间越长 duration=sprite_frame_arr.length/sample,
    @property(cc.Boolean)
    public loop: boolean = false;           // 是否循环播放
    @property(cc.Boolean)
    public play_onload: boolean = false;    // 是否在组件加载时播放

    // 组件属性
    public is_playing: boolean = false;     // 是否正在播放
    public play_time: number = 0;           // 动画时长

    public end_func: Function = null;       // 动画结束动画

    private _clip_name: string = "new_clip";

    public clip: cc.AnimationClip = null;

    start() {
        // 动画组件监听播放结束事件
        this.anim_com.on('stop', this.anim_stop_event, this);
    }

    onLoad() {
        this.is_playing = false;
        this.play_time = 0;

        if (this.play_onload) {
            this.create_anim_clip();
            this.play_anim();
        }
    }

    // 动画结束监听事件
    public anim_stop_event(e): void {
        // 执行动画结束回调
        if (this.end_func) {
            this.end_func();
        }
    }

    // 循环播放
    public play_loop(loop: boolean): void {
        this.create_anim_clip();
        if (loop) {
            this.loop = loop;
        }
        this.play_anim();
    }

    // 单次播放
    public play_once(end_func: Function): void {
        this.create_anim_clip();
        this.play_anim();
        this.end_func = end_func;
    }

    // 创建动画clip
    public create_anim_clip(): void {
        if (!this.sample) {
            this.sample = 10;
        }
        let clip: cc.AnimationClip = undefined;
        if (this.sprite_frame_arr.length > 0) {
            clip = cc.AnimationClip.createWithSpriteFrames(this.sprite_frame_arr, this.sample);
            this.clip = clip;
            // 给创建的动画命名
            this.anim_com.addClip(clip, this._clip_name);
        } else {
            cc.error("创建帧动画失败，精灵帧数组不存在");
        }
    }

    // 播放动画
    public play_anim(): void {
        let anim_state: cc.AnimationState = this.anim_com.play(this._clip_name);
        this.play_time = this.anim_com.currentClip.duration;
        // cc.log("当前动画的播放时长: ", this.play_time);
        if (this.loop) {    // 循环播放
            anim_state.wrapMode = cc.WrapMode.Loop;
        }
    }

    // 暂停动画
    public pause_anim(): void {
        this.anim_com.pause();
        this.is_playing = false;
    }

    // 恢复播放
    public resume_anim(): void {
        this.anim_com.resume();
        this.is_playing = true;
    }

    // 停止播放动画
    public stop_anim(): void {
        this.anim_com.stop();
        this.anim_com.removeClip(this.clip);
        this.is_playing = false;
        this.play_onload = false;
        this.loop = false;
        this.end_func = null;
    }
}
